import cache from '@/libs/cache'
import { getLanguage } from '@/locale'
import { SidebarStatus, FONT_SIZE, Language } from '../mutation-types'

export default {
  namespaced: true,
  state: {
    language: getLanguage(),
    sidebar: {
      opened: cache.get(SidebarStatus) ? !!+cache.get(SidebarStatus) : true,
      withoutAnimation: false
    },
    device: 'desktop',
    size: cache.get(FONT_SIZE) || 'medium'

  },
  mutations: {
    SET_LANGUAGE: (state, language) => {
      state.language = language
      cache.set(Language, language)
    },
    TOGGLE_SIDEBAR: state => {
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
      if (state.sidebar.opened) {
        cache.set(SidebarStatus, 1)
      } else {
        cache.set(SidebarStatus, 0)
      }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      cache.set(SidebarStatus, 0)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_SIZE: (state, size) => {
      state.size = size
      cache.set(FONT_SIZE, size)
    },



  },
  actions: {
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    toggleSideBar({ commit }) {
      commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    setSize({ commit }, size) {
      commit('SET_SIZE', size)
    },

  }
}